<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="70px">
      <el-row :gutter="24">
        <el-col :span="4">
          <el-form-item label="商品分类" prop="name">
            <el-input v-model="form.name" placeholder="请输入商品分类" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="loadData">搜索</el-button>
          <el-button @click="searchResetForm('form')">重 置</el-button>

        </el-col>
      </el-row>
    </el-form>

    <div style="margin-bottom: 20px;">
      <el-button type="primary" size="small" @click="add('formLabelAlign')">添加</el-button>
    </div>
    <Column :columns="columnLists" :rows="tableData" :loading="loading" :type="type" @selectionChange="selectionChange" @del="del" @edit="edit" />

    <Pagination :total="total" :current-page="pageNum" :page-size="pageSize" @handleChange="handleChange" />

    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="dialogVisible" width="45%">
      <el-form ref="formLabelAlign" :model="formLabelAlign" :rules="rules" label-width="140px">
        <el-row>
          <el-col :span="10">
            <el-form-item label="商品分类" prop="name">
              <el-input v-model="formLabelAlign.name" clearable />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="save('formLabelAlign')">确 定</el-button>
        <el-button size="mini" @click="resetForm('formLabelAlign')">重 置</el-button>
        <el-button size="mini" type="danger" @click="cancel('formLabelAlign')">关 闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import { showData, resetForm, validate, success, fail } from '@/api/common'
import Pagination from '@/components/Pagination'
import Column from '@/components/Column'
import { get, lists, post, del } from '@/api/requestCommon'

export default {
  name: 'PagePermission',
  components: {
    Pagination,
    Column
  },
  data() {
    return {
      pageNum: 1, pageSize: 10, total: 0,
      form: { name: '' },
      tableData: [],
      selectRows: [],
      dialogVisible: false,
      title: '添加商品分类',
      editId: '',
      columnLists: [
        { prop: 'name', label: '商品分类' },
        { prop: 'createTime', label: '创建时间' }
      ],
      type: 'class',
      rules: {
        name: [
          { required: true, message: '请输入商品分类', trigger: 'change' }
        ]
      },

      formLabelAlign: {
        id: '',
        name: ''
      },

      loading: true

    }
  },
  created() {

  },
  mounted() {
    this.loadData()
  },
  methods: {

    del(index) {
      this.$confirm('此操作将永久删除该商品分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del('category', this.tableData[index].id).then(response => {
          if (response.code === 200) {
            success(response.msg)
            this.loadData()
          } else {
            fail(response.msg)
          }
        })
      }).catch(() => {

      })
    },

    edit(index) {
      this.title = '编辑商品分类'
      this.dialogVisible = true
      const row = this.tableData[index]
      get('category/findById/' + row.id).then(response => {
        if (response.code === 200) {
          Object.assign(this.formLabelAlign, response.data)
        }
      })
    },
    cancel(formName) {
      resetForm(this, formName)
      this.dialogVisible = false
    },

    resetForm(formName) {
      resetForm(this, formName)
    },
    searchResetForm(formName) {
      resetForm(this, formName)
      this.loadData()
    },
    save(formName) {
      if (validate(this, formName)) {
        post('category/save', this.formLabelAlign).then(response => {
          if (response.code === 200) {
            success(response.msg)
            this.dialogVisible = false
            this.loadData()
          } else {
            fail(response.msg)
          }
        })
      }
    },
    add(formName) {
      this.title = '添加商品分类'
      this.$nextTick(() => {
        resetForm(this, formName)
      })
      this.dialogVisible = true
    },

    selectionChange(rows) {
      this.selectRows = rows
    },

    handleChange(pageNum, pageSize) {
      this.pageNum = pageNum
      this.pageSize = pageSize
      this.loadData()
    },

    loadData() {
      this.form.pageNum = this.pageNum
      this.form.pageSize = this.pageSize
      lists('category', this.form).then(response => {
        showData(response, this)
      })
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 140px;
  height: 140px;
  line-height: 140px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.avatar-uploader img {
  cursor: pointer;
  width: 100px;
  border-radius: 50px;
  height: 100px;
}

</style>
